package cn.tedu.oa.procedure.service.impl;


import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.procedure.dao.ProcedureDao;
import cn.tedu.oa.procedure.entity.Procedure;
import cn.tedu.oa.procedure.service.ProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;

@Service
public  class ProcedureServiceImpl extends BaseServiceImpl<Procedure> implements ProcedureService {
    @Autowired
    private ProcedureDao procedureDao ;

    @Override
    public List<Procedure> getProcedurePagerList(String criteria, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
            throw new IllegalArgumentException("参数不合法，请联系运维人员");
        }
        int startIndex = (pageNo - 1) * pageSize + 1;
        int endIndex = startIndex + pageSize - 1;
        List<Procedure> list = procedureDao.getProcedurePagerList(criteria, startIndex, endIndex);
        if (list == null) {
            throw new ServiceException("没有查询到数据");
        }
        return list;
    }

    @Override
    public Procedure getProcedureById(Integer id) {
        if (id == null || id < 0) {
            return null;
        }
        return procedureDao.get(id);
    }

    @Override
    public int updateByIds(Integer... ids) {
        if (ids == null || ids.length == 0) {
            throw new IllegalArgumentException("参数不合法");
        }
        int rows = procedureDao.updateByIds(ids);
        if (rows == 0){
            throw new ServiceException("审核失败：信息为空");
        }
        return rows;
    }

    /*新增流程*/
    @Override
    public void add(Procedure procedure) {
        if (procedure == null) {
            throw new ServiceException("添加失败：用户信息为空");
        }
        if (StringUtils.isEmpty(procedure.getTitle().trim())) {
            throw new ServiceException("添加失败：标题为空");
        }
        if (StringUtils.isEmpty(procedure.getType().trim())) {
            throw new ServiceException("添加失败：类型为空");
        }
        if (StringUtils.isEmpty(procedure.getName().trim())) {
            throw new ServiceException("添加失败：申请人为空");
        }
        /*添加人*/
        procedure.setAddUser(ShiroUtils.getPrincipal().getName());
        /*添加时间*/
        procedure.setAddTime(new Date());
        /*删除状态：0正常，1已删除*/
        procedure.setIsdel(0);
        procedureDao.add(procedure);
    }


    @Override
    public void update(Procedure procedure) {
        if (procedure == null) {
            throw new ServiceException("添加失败：用户信息为空");
        }
        if (StringUtils.isEmpty(procedure.getTitle().trim())) {
            throw new ServiceException("添加失败：标题为空");
        }
        if (StringUtils.isEmpty(procedure.getType().trim())) {
            throw new ServiceException("添加失败：类型为空");
        }
        if (StringUtils.isEmpty(procedure.getName().trim())) {
            throw new ServiceException("添加失败：申请人为空");
        }

        /*修改人*/
        procedure.setUpdateUser(ShiroUtils.getPrincipal().getName());
        /*修改时间*/
        procedure.setUpdateTime(new Date());
        /*删除状态：0正常，1已删除*/
        procedure.setIsdel(0);
        procedureDao.update(procedure);
    }
}
